<?php
// +----------------------------------------------------------------------
// | Created by PHPstorm: [ JRK丶Admin ]
// +----------------------------------------------------------------------
// | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.luckyhhy.cn
// +----------------------------------------------------------------------
// | Author: LuckyHhy <jackhhy520@qq.com>
// +----------------------------------------------------------------------
// | Date: 2020/6/26 0026
// +----------------------------------------------------------------------
// | Description:
// +----------------------------------------------------------------------

namespace app\admin\controller;




use app\common\controller\AdminBaseController;
use app\common\model\tang\BalanceLog;
use app\common\model\tang\Feedback as FeedbackModel;
use app\common\model\tang\FeedbackDetail;
use think\facade\Db;

class Feedback extends AdminBaseController
{
    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->model = new FeedbackModel();
    }


    /**
     * @return mixed|string|\think\response\Json
     * @throws \think\db\exception\DbException
     * @author: Hhy <jackhhy520@qq.com>
     * @date: 2020/6/30 0030
     * @describe:添加编辑
     */
    public function add()
    {
        if (IS_AJAX) {
            $data = $this->request->post();
            return $this->model->doAll($data);
        }
        $id = $this->request->param("id/d");//父id
        $info = $this->model->find($id);
        //获取菜单列表
        $this->assign(compact("info", "id"));
        return $this->fetch();
    }


    public function detail()
    {
        $id = $this->request->param("id/d");//父id
        $t = $this->model->with(['back'])->find($id)->toArray();
        foreach ($t['back'] as& $v){
            $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
        }
        $this->assign(compact("t"));
        return $this->fetch();
    }


    // 办结
    public function end()
    {
        if (IS_AJAX) {
            $data = $this->request->post();
            $info = $this->model->find($data['id']);
            if($info){
                $remark = $data['remark'] ;
                if(empty($remark)){
                    return $this->model->doAjsx('-1',[],'内容不能为空');
                }
                Db::startTrans();
                try {
                    $this->model->update(['status'=>2,'result'=>$remark,'update_time'=>time()],['id'=>$data['id']]);
                    Db::commit();
                    return self::JsonReturn("处理成功");
                }catch (\Exception $exception){
                    Db::rollback();
                    return self::JsonReturn($exception->getMessage(),0);
                }
            }else{
                return parent::JsonReturn('记录不存在', 1);
            }
        }
        $id = $this->request->param("id/d");//父id
        $info = $this->model->find($id);
        $this->assign(compact("info"));
        return $this->fetch();
    }
    // 回复

    public function reply()
    {
        if (IS_AJAX) {
            $detail = $this->request->post('detail');

            if(empty($detail)){
                return $this->model->doAjsx('-1',[],'回复内容不能为空');
            }else{
                $pid = $this->request->post('pid');
                $info = $this->model->find($pid);
                if($info->status==0){
                    $this->model->update(['status'=>1],['id'=>$pid]);
                }elseif ($info->status==2){
                    return $this->model->doAjsx('-1',[],'已办结');
                }
                 // 记录回复内容
                $data = [
                     'pid'         => $pid ,
                     'is_admin'    => 1 ,
                     'detail'      => $detail ,
                     'create_time' => time(),
                     'update_time' => time()
                ];
                return (new FeedbackDetail())->doAll($data);
            }
        }else{
            var_dump(123123);
        }
    }

}